/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import entity.Utenti;
import java.io.IOException;
import java.io.StringReader;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

/**
 *
 * @author erola
 */
public class ListaUtenti extends HttpServlet {

    /** 
     * Servlet utilizzata per la visualizzazione degli utenti, tramite web service,
     * iscritti su WishListerSupport
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException se avviene un errore a livello di servlet
     * @throws IOException se avviene un errore I/O
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        Utenti u = null;
        try {
            wishlister.WLWebServiceService service = new wishlister.WLWebServiceService();
            wishlister.WLWebService port = service.getWLWebServicePort();
            String result = port.listaUtenti();

            //////////////////////////////////////

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = factory.newDocumentBuilder();
            InputSource inStream = new InputSource();
            inStream.setCharacterStream(new StringReader(result));
            Document doc = db.parse(inStream);

            doc.getDocumentElement().normalize();
            NodeList nodeLst = doc.getElementsByTagName("utente");

            for (int s = 0; s < nodeLst.getLength(); s++) {

                Node fstNode = nodeLst.item(s);

                if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element fstElmnt = (Element) fstNode;

                    NodeList uidElmntLst = fstElmnt.getElementsByTagName("id");
                    Element uidElmnt = (Element) uidElmntLst.item(0);
                    NodeList uidNode = uidElmnt.getChildNodes();
                    Long uid = Long.valueOf(((Node) uidNode.item(0)).getNodeValue());

                    NodeList loginElmntLst = fstElmnt.getElementsByTagName("login");
                    Element loginElmnt = (Element) loginElmntLst.item(0);
                    NodeList loginNode = loginElmnt.getChildNodes();
                    String login = ((Node) loginNode.item(0)).getNodeValue();

                    NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("nome");
                    Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
                    NodeList fstNm = fstNmElmnt.getChildNodes();
                    String nome = ((Node) fstNm.item(0)).getNodeValue();

                    NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("cognome");
                    Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
                    NodeList lstNm = lstNmElmnt.getChildNodes();
                    String cognome = ((Node) lstNm.item(0)).getNodeValue();

                    if (request.getParameter("username").equals(login)) {
                        u = new Utenti();
                        u.setId(uid);
                        u.setLogin(login);
                        u.setNome(nome);
                        u.setCognome(cognome);
                        request.setAttribute("utente", nome + " " + cognome);
                        request.setAttribute("username", login);
                        request.setAttribute("uid", uid);
                        break;
                    }
                }
               


            }

            //////////////////////////////////////

        } catch (Exception e) {
            request.setAttribute("err", "Errore: impossibile recuperare la lista degli utenti WishLister.");
        } finally {
            String dispatch;
            if(u == null){
                request.setAttribute("err", "Utente non trovato.");
                dispatch = "/utenti.jsp";
            } else {
                dispatch = "ListaRegali";
            }
            RequestDispatcher rq = request.getRequestDispatcher(dispatch);
            rq.forward(request, response);
        }
    }

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);


    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);


    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";

    }// </editor-fold>
}
